package com.asset.dispoal.business.bsAgreement.service.commom;

import com.asset.dispoal.business.bsAgreement.domain.db.entity.Agreement;
import com.asset.dispoal.business.bsAgreement.domain.db.entity.AgreementExpand;
import com.asset.dispoal.business.bsAgreement.domain.db.mapper.AgreementMapper;
import com.asset.dispoal.business.bsAgreement.dto.AgreementDataRequest;
import com.asset.dispoal.business.bsAgreement.service.converter.AgreementConverter;
import com.asset.dispoal.business.bsBorrowDetail.domain.db.entity.BorrowDetail;
import com.asset.dispoal.business.bsBorrowDetail.domain.db.mapper.BsBorrowDetailMapper;
import com.asset.dispoal.business.bsBorrower.domain.db.entity.Borrower;
import com.asset.dispoal.business.bsBorrower.domain.db.mapper.BorrowerMapper;
import com.asset.dispoal.business.bsCase.domain.db.entity.BsCase;
import com.asset.dispoal.business.bsCase.domain.db.mapper.BsCaseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.util.StringUtil;
import tech.mhuang.pacebox.springboot.protocol.Result;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author qr
 * @date 2022/5/5$
 */
@Component
public class UpdateAgreementCommand implements BaseCommand<AgreementDataRequest, Result> {

    @Autowired
    AgreementMapper agreementMapper;

    @Autowired
    BsBorrowDetailMapper bsBorrowDetailMapper;

    @Autowired
    BorrowerMapper borrowerMapper;
    @Autowired
    BsCaseMapper bsCaseMapper;


    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Result execute(AgreementDataRequest agreementDataRequest) {
        Agreement agreement = AgreementConverter.INSTANCE.reqDataToAgreement(agreementDataRequest);
        AgreementExpand agreementExpand = AgreementConverter.INSTANCE.reqDataToAgreementExpand(agreementDataRequest);
        BorrowDetail borrowDetail = AgreementConverter.INSTANCE.reqDataToBorrowDetail(agreementDataRequest);
        Borrower borrower = AgreementConverter.INSTANCE.reqDataToBorrower(agreementDataRequest);

        List<BorrowDetail> borrowDetails = bsBorrowDetailMapper.queryBorrowDetail(agreement.getId());
        BigDecimal lawsuitPrincipalAmountSum = borrowDetails.stream().map(BorrowDetail::getLawsuitPrincipalAmount).map(BigDecimal::new).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal creditPrincipalAmountSum = borrowDetails.stream().map(BorrowDetail::getCreditPrincipalAmount).filter(StringUtil::isNotBlank).map(BigDecimal::new).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal lawsuitInterestAmountSum = borrowDetails.stream().map(BorrowDetail::getLawsuitInterestAmount).filter(StringUtil::isNotBlank).map(BigDecimal::new).reduce(BigDecimal.ZERO, BigDecimal::add);

        BsCase caseDto = new BsCase();
        caseDto.setLawsuitPrincipalAmount(lawsuitPrincipalAmountSum);
        caseDto.setCreditPrincipalAmount(creditPrincipalAmountSum);
        caseDto.setLawsuitInterestAmount(lawsuitInterestAmountSum);


        bsCaseMapper.updateCaseAmount(agreement.getId(), caseDto);
        agreementMapper.updateAgreementExpand(agreementExpand);
        agreementMapper.updateAgreement(agreement);
        bsBorrowDetailMapper.updateBorrowDetail(borrowDetail);
        borrowerMapper.updateByAgreementId(borrower);

        return Result.ok();
    }

    public static void main(String[] args) {


    }
}
